layui.define(["app"], function (exports) {
    "use strict";

    // 公共函数
    let dropdown = layui.dropdown;
    let jquery = layui.$
    let app = layui.app

    // 进入全屏
    let fullScreen = () => {
        var el = document.documentElement;
        var rfs = el.requestFullScreen || el.webkitRequestFullScreen;
        if (typeof rfs != "undefined" && rfs) {
            rfs.call(el);
        } else if (typeof window.ActiveXObject != "undefined") {
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript != null) {
                wscript.SendKeys("{F11}");
            }
        } else if (el.msRequestFullscreen) {
            el.msRequestFullscreen();
        } else if (el.oRequestFullscreen) {
            el.oRequestFullscreen();
        } else if (el.webkitRequestFullscreen) {
            el.webkitRequestFullscreen();
        } else if (el.mozRequestFullScreen) {
            el.mozRequestFullScreen();
        } else {
            console.error('浏览器不支持全屏调用！');
        }
    }

    // 退出全屏
    let exitFullScreen = () => {
        var el = document;
        var cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.exitFullScreen;
        if (typeof cfs != "undefined" && cfs) {
            cfs.call(el);
        } else if (typeof window.ActiveXObject != "undefined") {
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript != null) {
                wscript.SendKeys("{F11}");
            }
        } else if (el.msExitFullscreen) {
            el.msExitFullscreen();
        } else if (el.oRequestFullscreen) {
            el.oCancelFullScreen();
        } else if (el.mozCancelFullScreen) {
            el.mozCancelFullScreen();
        } else if (el.webkitCancelFullScreen) {
            el.webkitCancelFullScreen();
        } else {
            miniAdmin.error('浏览器不支持全屏调用！');
        }
    }

    let fn = {
        home: {
            left_menu: function ($) {
                $(".left-menu").delegate("a", "click", function () {
                    const i = $(".left-menu").find("i")
                    console.log(i.hasClass("layui-icon-shrink-right"))
                    if (i.hasClass("layui-icon-shrink-right")) {
                        $(".layui-layout").addClass("admin-mini")
                        i.removeClass("layui-icon-shrink-right")
                        i.addClass("layui-icon-spread-left")
                    } else {
                        $(".layui-layout").removeClass("admin-mini")
                        i.removeClass("layui-icon-spread-left")
                        i.addClass("layui-icon-shrink-right")
                    }
                })
                $(".layui-nav-tree .layui-nav-item>a").on("click", function () {
                    $(".admin-mini").removeClass("admin-mini")
                    $(".admin-mini .left-menu i").removeClass("layui-icon-spread-left")
                        .addClass("layui-icon-shrink-right")
                })
                $(".pe-left-menu").click(function () {
                    let i = $(".pe-left-menu").find("i")
                    if (i.hasClass("layui-icon-shrink-right")) {
                        i.removeClass("layui-icon-shrink-right")
                        i.addClass("layui-icon-spread-left")
                        $(".admin-pe .layui-logo").css("left", "-220px")
                        $(".admin-pe .layui-side").css("left", "-220px")
                    } else {
                        i.removeClass("layui-icon-spread-left")
                        i.addClass("layui-icon-shrink-right")
                        $(".admin-pe .layui-logo").css("left", "0px")
                        $(".admin-pe .layui-side").css("left", "0px")
                    }
                })
                function handleResize() {
                    if ($(window).width() < 992) {
                        $(".layui-layout").addClass("admin-pe")
                        $(".pe-left-menu").click()
                    } else {
                        $(".admin-pe .layui-logo").css("left", "0")
                        $(".admin-pe .layui-side").css("left", "0")
                        $(".layui-layout").removeClass("admin-pe")
                    }
                }
                handleResize()
                $(window).resize(handleResize);
            },
            pagetabs: function ($) {
                const scrollStep = 50;
                const leftButton = $(".layui-pagetabs .prev")
                const rightButton = $(".layui-pagetabs .next")
                const scrollableDiv = $(".layui-pagetabs .pagetabs-nav")
                const bodyConetnt = $(".layui-pagetabs-body")
                leftButton.click(function () {
                    scrollableDiv.scrollLeft(scrollableDiv.scrollLeft() - scrollStep)
                })
                rightButton.click(function () {
                    scrollableDiv.scrollLeft(scrollableDiv.scrollLeft() + scrollStep);
                });
                dropdown.render({
                    elem: $(".layui-pagetabs .down"),
                    align: "right",
                    data: [
                        {
                            title: "关闭当前标签页",
                            id: 0
                        }, {
                            title: "关闭其他标签页",
                            id: 1
                        }, {
                            title: "关闭右侧标签页",
                            id: 2
                        }, {
                            title: "关闭全部标签页",
                            id: 3
                        }
                    ],
                    click: function (data, othis, event) {
                        let this_nav = scrollableDiv
                            .find(".layui-this")

                        switch (data.id) {
                            case 0:
                                this_nav.find(".layui-icon-close").click()
                                break;
                            case 1:
                                scrollableDiv.children(`li[class][class!="layui-this"]`)
                                    .each(function (k, v) {
                                        $(v).find(".layui-icon-close").click()
                                    })
                                break;
                            case 2:
                                scrollableDiv.children(`li`)
                                    .each(function (k, v) {
                                        if (k > this_nav.index()) {
                                            $(v).find(".layui-icon-close").click()
                                        }
                                    })
                                break;
                            default:
                                scrollableDiv.children(`li`)
                                    .each(function (k, v) {
                                        $(v).find(".layui-icon-close").click()
                                    })
                                break;
                        }
                    }
                })
                $(".pagetabs-add a").on("click", function () {
                    // console.log(this)
                    const tag_name = $(this).attr("tag")
                    const content = $(this).html()
                    const is_tab = scrollableDiv.find(`[tag="${tag_name}"]`)
                    // console.log(is_tab.length)
                    const path = $(this).attr("path")
                    if (typeof path != "undefined" && path !== "undefined") {
                        scrollableDiv.find(".layui-this").removeClass("layui-this")
                        app.component(tag_name, path)
                        if (is_tab.length === 0) {
                            scrollableDiv.append(`
                                <li class="layui-this" tag="${tag_name}" path="${path}">
                                    <a href="javascript:;">
                                        ${content}
                                        <i class="layui-icon layui-icon-close"></i>
                                    </a>
                                </li>`)
                        } else {
                            is_tab.addClass("layui-this")
                        }
                    } else {
                        return
                    }
                    if (bodyConetnt.find(tag_name).length === 0) {
                        bodyConetnt.html(`<${tag_name}></${tag_name}>`)
                    }
                })
                $(".layui-pagetabs .pagetabs-nav").delegate("li", "click", function (event) {
                    if (!$(event.target).is('.layui-icon-close')) {
                        const tag_name = $(this).attr("tag")
                        scrollableDiv.find(".layui-this").removeClass("layui-this")
                        $(this).addClass("layui-this")
                        if (bodyConetnt.find(tag_name).length === 0) {
                            bodyConetnt.html(`<${tag_name}></${tag_name}>`)
                        }
                    }
                })
                $(".layui-pagetabs").delegate(".layui-icon-close", "click", function () {
                    const parent = $(this).parent().parent()
                    const next = scrollableDiv.children().eq(0)
                    parent.remove()
                    if (parent.hasClass("layui-this")) {
                        scrollableDiv.find(".layui-this").removeClass("layui-this")
                        next.addClass("layui-this")
                        bodyConetnt.html(`<${next.attr("tag")}></${next.attr("tag")}>`)
                    }
                })
            },
            screen: function ($) {
                $(".screen").click(function () {
                    let then = $(this).children()
                    if (then.hasClass("layui-icon-screen-full")) {
                        fullScreen()
                        then.removeClass("layui-icon-screen-full")
                        then.addClass("layui-icon-screen-restore")
                    } else {
                        exitFullScreen()
                        then.removeClass("layui-icon-screen-restore")
                        then.addClass("layui-icon-screen-full")
                    }
                })
            },
            logOut: function () {
                // 清理token
                layui.data('user', null)
                layui.layer.msg("注销成功", { icon: 1 }, function () {
                    window.location.href = "#/login"
                })
            }
        },
        /**
         * 获取表单数据
         * @param {object} elem jq对象
         * @returns object
         */
        formData: function (elem) {
            const data = elem.serializeArray()
            let form_json = {}
            jquery.map(data, function (item) {
                if (form_json[item.name]) {
                    if (!Array.isArray(form_json[item.name])) {
                        form_json[item.name] = [form_json[item.name]];
                    }
                    form_json[item.name].push(item.value);
                } else {
                    form_json[item.name] = item.value;
                }
            })
            return form_json
        }
    }

    exports('fn', fn); // 输出模块
}); 